package ca.ubc.icapture.genapha.forms;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorForm;

/**
 *
 * @author phan
 */
public class SearchForm extends ValidatorForm
{

    private Integer searchSelect;
    private String searchText;
    private String submitType;
    private String snps;
    private String genes;
    private String chromosomes;
    private String pathways;
    private Integer selectedPubli;
    private Integer selectedPendPubli;
    private ArrayList<String> availablePubliNames = new ArrayList<String>();
    private ArrayList<Integer> availablePubliIds = new ArrayList<Integer>();
    private ArrayList<String> availablePendPubliNames = new ArrayList<String>();
    private ArrayList<Integer> availablePendPubliIds = new ArrayList<Integer>();

    public SearchForm()
    {
        super();

        // TODO: Populates availablePubli from DB
        availablePubliNames.add("<--Select-->");
        availablePubliIds.add(-1);
        availablePubliNames.add("Analyses of associations with asthma in four asthma population samples from Canada and Australia");
        availablePubliIds.add(0);
        availablePubliNames.add("Association of Urokinase-type Plasminogen Activator with Asthma and Atopy");
        availablePubliIds.add(7);
        availablePubliNames.add("A Thymic Stromal Lymphopoietin Gene Variant Is Associated with Asthma and Airway Hyperresponsiveness");
        availablePubliIds.add(9);
        availablePubliNames.add("Genetic variation in immune signaling genes differentially expressed in asthmatic lung tissues");
        availablePubliIds.add(8);
        availablePubliNames.add("Lack of Association of TIM3 Polymorphisms and Allergic Phenotypes");
        availablePubliIds.add(1);

        // TODO: Populates availablePendPubli from DB
        availablePendPubliNames.add("Currently there are no pending papers.");
        availablePendPubliIds.add(-1);
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        searchText = "";
        submitType = "";
        snps = "";
        genes = "";
        chromosomes = "";
        pathways = "";
        selectedPubli = -1;
        selectedPendPubli = -1;
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = super.validate(mapping, request);

        if (submitType.equalsIgnoreCase("Search"))
        {
            boolean isValid = true;
            switch (searchSelect)
            {
                case 0:
                    // All search
                    if (searchText.isEmpty())
                    {
                        isValid = false;
                    }
                    break;
                case 1:
                    // SNP search
                    if (searchText.isEmpty() && genes.isEmpty()
                            && chromosomes.isEmpty() && pathways.isEmpty())
                    {
                        isValid = false;
                    }
                    break;
                case 2:
                    // Gene search
                    if (searchText.isEmpty() && chromosomes.isEmpty()
                            && pathways.isEmpty() && selectedPubli == -1 && selectedPendPubli == -1)
                    {
                        isValid = false;
                    }
                    break;
                case 3:
                    // Pathway search
                    if (searchText.isEmpty() && snps.isEmpty() && pathways.isEmpty())
                    {
                        isValid = false;
                    }
                    break;
                default:
                    break;
            }
            if (!isValid)
            {
                errors.add("search", new ActionMessage("search.error"));
            }
        }

        return errors;
    }

    /**
     * @return the searchSelect
     */
    public Integer getSearchSelect()
    {
        return searchSelect;
    }

    /**
     * @param searchSelect the searchSelect to set
     */
    public void setSearchSelect(Integer searchSelect)
    {
        this.searchSelect = searchSelect;
    }

    /**
     * @return the searchText
     */
    public String getSearchText()
    {
        return searchText;
    }

    /**
     * @param searchText the searchText to set
     */
    public void setSearchText(String searchText)
    {
        this.searchText = searchText;
    }

    /**
     * @return the submitType
     */
    public String getSubmitType()
    {
        return submitType;
    }

    /**
     * @param submitType the submitType to set
     */
    public void setSubmitType(String submitType)
    {
        this.submitType = submitType;
    }

    /**
     * @return the snp
     */
    public String getSnps()
    {
        return snps;
    }

    /**
     * @param snp the snp to set
     */
    public void setSnps(String snp)
    {
        this.snps = snp;
    }

    /**
     * @return the gene
     */
    public String getGenes()
    {
        return genes;
    }

    /**
     * @param gene the gene to set
     */
    public void setGenes(String gene)
    {
        this.genes = gene;
    }

    /**
     * @return the chromosome
     */
    public String getChromosomes()
    {
        return chromosomes;
    }

    /**
     * @param chromosome the chromosome to set
     */
    public void setChromosomes(String chromosome)
    {
        this.chromosomes = chromosome;
    }

    /**
     * @return the selectedPathway
     */
    public String getPathways()
    {
        return pathways;
    }

    /**
     * @param pathways the selectedPathway to set
     */
    public void setPathways(String pathways)
    {
        this.pathways = pathways;
    }

    /**
     * @return the selectedPubli
     */
    public Integer getSelectedPubli()
    {
        return selectedPubli;
    }

    /**
     * @param selectedPubli the selectedPubli to set
     */
    public void setSelectedPubli(Integer selectedPubli)
    {
        this.selectedPubli = selectedPubli;
    }

    /**
     * @return the selectedPendPubli
     */
    public Integer getSelectedPendPubli()
    {
        return selectedPendPubli;
    }

    /**
     * @param selectedPendPubli the selectedPendPubli to set
     */
    public void setSelectedPendPubli(Integer selectedPendPubli)
    {
        this.selectedPendPubli = selectedPendPubli;
    }

    /**
     * @return the availablePubliNames
     */
    public ArrayList<String> getAvailablePubliNames()
    {
        return availablePubliNames;
    }

    /**
     * @param availablePubliNames the availablePubliNames to set
     */
    public void setAvailablePubliNames(ArrayList<String> availablePubliNames)
    {
        this.availablePubliNames = availablePubliNames;
    }

    /**
     * @return the availablePubliIds
     */
    public ArrayList<Integer> getAvailablePubliIds()
    {
        return availablePubliIds;
    }

    /**
     * @param availablePubliIds the availablePubliIds to set
     */
    public void setAvailablePubliIds(ArrayList<Integer> availablePubliIds)
    {
        this.availablePubliIds = availablePubliIds;
    }

    /**
     * @return the availablePendPubliNames
     */
    public ArrayList<String> getAvailablePendPubliNames()
    {
        return availablePendPubliNames;
    }

    /**
     * @param availablePendPubliNames the availablePendPubliNames to set
     */
    public void setAvailablePendPubliNames(ArrayList<String> availablePendPubliNames)
    {
        this.availablePendPubliNames = availablePendPubliNames;
    }

    /**
     * @return the availablePendPubliIds
     */
    public ArrayList<Integer> getAvailablePendPubliIds()
    {
        return availablePendPubliIds;
    }

    /**
     * @param availablePendPubliIds the availablePendPubliIds to set
     */
    public void setAvailablePendPubliIds(ArrayList<Integer> availablePendPubliIds)
    {
        this.availablePendPubliIds = availablePendPubliIds;
    }
}
